
<!DOCTYPE HTML>
<html lang="" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>关联关系 · GitBook</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.3">
        
        
        
    
    <link rel="stylesheet" href="../../gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-search/search.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-fontsettings/website.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../../gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="../../gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="querying.html" />
    
    
    <link rel="prev" href="changesets.html" />
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="Type to search" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        <li class="header">第一部分：基础</li>
        
        
    
        <li class="chapter " data-level="1.1" data-path="../../">
            
                <a href="../../">
            
                    
                    Introduction
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="../basics/basics.html">
            
                <a href="../basics/basics.html">
            
                    
                    基础
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="../basics/collections.html">
            
                <a href="../basics/collections.html">
            
                    
                    集合
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4" data-path="../basics/enum.html">
            
                <a href="../basics/enum.html">
            
                    
                    Enum 模块
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5" data-path="../basics/pattern-matching.html">
            
                <a href="../basics/pattern-matching.html">
            
                    
                    模式匹配
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6" data-path="../basics/control-structures.html">
            
                <a href="../basics/control-structures.html">
            
                    
                    控制语句
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7" data-path="../basics/functions.html">
            
                <a href="../basics/functions.html">
            
                    
                    函数
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8" data-path="../basics/pipe-operator.html">
            
                <a href="../basics/pipe-operator.html">
            
                    
                    管道操作符
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.9" data-path="../basics/modules.html">
            
                <a href="../basics/modules.html">
            
                    
                    模块(Module)
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.10" data-path="../basics/mix.html">
            
                <a href="../basics/mix.html">
            
                    
                    Mix
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.11" data-path="../basics/sigils.html">
            
                <a href="../basics/sigils.html">
            
                    
                    魔符(Sigil)
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.12" data-path="../basics/documentation.html">
            
                <a href="../basics/documentation.html">
            
                    
                    文档模块
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.13" data-path="../basics/testing.html">
            
                <a href="../basics/testing.html">
            
                    
                    测试
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.14" data-path="../basics/comprehensions.html">
            
                <a href="../basics/comprehensions.html">
            
                    
                    推导
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.15" data-path="../basics/strings.html">
            
                <a href="../basics/strings.html">
            
                    
                    字符串
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.16" data-path="../basics/date-time.html">
            
                <a href="../basics/date-time.html">
            
                    
                    日期和时间
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.17" data-path="../basics/mix-tasks.html">
            
                <a href="../basics/mix-tasks.html">
            
                    
                    自定义Mix任务
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.18" data-path="../basics/iex-helpers.html">
            
                <a href="../basics/iex-helpers.html">
            
                    
                    IEx辅助函数
            
                </a>
            

            
        </li>
    

    
        
        <li class="header">第二部分：高级</li>
        
        
    
        <li class="chapter " data-level="2.1" data-path="../advanced/erlang.html">
            
                <a href="../advanced/erlang.html">
            
                    
                    和Erlang互操作
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.2" data-path="../advanced/error-handling.html">
            
                <a href="../advanced/error-handling.html">
            
                    
                    错误处理
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.3" data-path="../advanced/escripts.html">
            
                <a href="../advanced/escripts.html">
            
                    
                    可执行文件
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.4" data-path="../advanced/concurrency.html">
            
                <a href="../advanced/concurrency.html">
            
                    
                    并发
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.5" data-path="../advanced/otp-concurrency.html">
            
                <a href="../advanced/otp-concurrency.html">
            
                    
                    OTP并发
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.6" data-path="../advanced/otp-supervisors.html">
            
                <a href="../advanced/otp-supervisors.html">
            
                    
                    OTP Supervisors
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.7" data-path="../advanced/otp-distribution.html">
            
                <a href="../advanced/otp-distribution.html">
            
                    
                    OTP 分布式
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.8" data-path="../advanced/metaprogramming.html">
            
                <a href="../advanced/metaprogramming.html">
            
                    
                    元编程
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.9" data-path="../advanced/umbrella-projects.html">
            
                <a href="../advanced/umbrella-projects.html">
            
                    
                    Umbrella Projects
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.10" data-path="../advanced/typespec.html">
            
                <a href="../advanced/typespec.html">
            
                    
                    Specifications and types
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.11" data-path="../advanced/behaviours.html">
            
                <a href="../advanced/behaviours.html">
            
                    
                    行为
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.12" data-path="../advanced/gen-stage.html">
            
                <a href="../advanced/gen-stage.html">
            
                    
                    GenStage
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.13" data-path="../advanced/protocols.html">
            
                <a href="../advanced/protocols.html">
            
                    
                    协议
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.14" data-path="../advanced/nerves.html">
            
                <a href="../advanced/nerves.html">
            
                    
                    Nerves
            
                </a>
            

            
        </li>
    

    
        
        <li class="header">第三部分：ECTO</li>
        
        
    
        <li class="chapter " data-level="3.1" data-path="basics.html">
            
                <a href="basics.html">
            
                    
                    Basics
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.2" data-path="changesets.html">
            
                <a href="changesets.html">
            
                    
                    Changesets
            
                </a>
            

            
        </li>
    
        <li class="chapter active" data-level="3.3" data-path="associations.html">
            
                <a href="associations.html">
            
                    
                    关联关系
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.4" data-path="querying.html">
            
                <a href="querying.html">
            
                    
                    查询
            
                </a>
            

            
        </li>
    

    
        
        <li class="header">第四部分：专题</li>
        
        
    
        <li class="chapter " data-level="4.1" data-path="../specifics/plug.html">
            
                <a href="../specifics/plug.html">
            
                    
                    Plug
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.2" data-path="../specifics/eex.html">
            
                <a href="../specifics/eex.html">
            
                    
                    嵌入的 Elixir(EEx)
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.3" data-path="../specifics/ets.html">
            
                <a href="../specifics/ets.html">
            
                    
                    Erlang 项式存储(ETS)
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.4" data-path="../specifics/mnesia.html">
            
                <a href="../specifics/mnesia.html">
            
                    
                    Mnesia 数据库
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.5" data-path="../specifics/debugging.html">
            
                <a href="../specifics/debugging.html">
            
                    
                    调试
            
                </a>
            

            
        </li>
    

    
        
        <li class="header">第五部分：程序库</li>
        
        
    
        <li class="chapter " data-level="5.1" data-path="../libraries/guardian.html">
            
                <a href="../libraries/guardian.html">
            
                    
                    Guardian(基础)
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="5.2" data-path="../libraries/poolboy.html">
            
                <a href="../libraries/poolboy.html">
            
                    
                    Poolboy
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="5.3" data-path="../libraries/benchee.html">
            
                <a href="../libraries/benchee.html">
            
                    
                    Benchee
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="5.4" data-path="../libraries/bypass.html">
            
                <a href="../libraries/bypass.html">
            
                    
                    Bypass
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="5.5" data-path="../libraries/distillery.html">
            
                <a href="../libraries/distillery.html">
            
                    
                    Distillery(基础)
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="5.6" data-path="../libraries/stream-data.html">
            
                <a href="../libraries/stream-data.html">
            
                    
                    StreamData
            
                </a>
            

            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            Published with GitBook
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="../.." >关联关系</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <p>&#x672C;&#x8BFE;&#x7A0B;&#x6211;&#x4EEC;&#x5C06;&#x5B66;&#x4E60;&#x5982;&#x4F55;&#x4F7F;&#x7528; Ecto &#x6765;&#x5B9A;&#x4E49;&#x548C;&#x4F7F;&#x7528; schema &#x4E4B;&#x95F4;&#x7684;&#x5173;&#x8054;&#x5173;&#x7CFB;&#x3002;</p>
<h2 id="&#x914D;&#x7F6E;">&#x914D;&#x7F6E;</h2>
<p>&#x6211;&#x4EEC;&#x5C06;&#x57FA;&#x4E8E;&#x524D;&#x9762;&#x8BFE;&#x7A0B;&#x642D;&#x5EFA;&#x7684; app, <code>Example</code>&#xFF0C;&#x6765;&#x64CD;&#x4F5C;&#x3002;&#x4F60;&#x53EF;&#x4EE5;&#x901A;&#x8FC7;<a href="../basics">&#x8FD9;&#x91CC;</a>&#x6765;&#x56DE;&#x987E;&#x4E00;&#x4E0B;&#x3002;</p>
<h2 id="&#x5173;&#x8054;&#x7684;&#x79CD;&#x7C7B;">&#x5173;&#x8054;&#x7684;&#x79CD;&#x7C7B;</h2>
<p>Schema &#x4E4B;&#x95F4;&#x7684;&#x5173;&#x8054;&#x5173;&#x7CFB;&#x6709;&#x4E09;&#x79CD;&#x3002;&#x6211;&#x4EEC;&#x5C06;&#x9010;&#x4E2A;&#x6765;&#x770B;&#x4ED6;&#x4EEC;&#x662F;&#x4EC0;&#x4E48;&#xFF0C;&#x5E76;&#x5982;&#x4F55;&#x5B9E;&#x73B0;&#x3002;</p>
<h3 id="&#x5C5E;&#x4E8E;&#x4E00;&#x5BF9;&#x591A;">&#x5C5E;&#x4E8E;/&#x4E00;&#x5BF9;&#x591A;</h3>
<p>&#x6211;&#x4EEC;&#x9700;&#x8981;&#x5148;&#x5F80;&#x6211;&#x4EEC;&#x7684;&#x793A;&#x8303;&#x9879;&#x76EE;&#x91CC;&#x6DFB;&#x52A0;&#x4E00;&#x4E9B;&#x65B0;&#x7684;&#x6A21;&#x578B;&#x5B9E;&#x4F8B;&#xFF0C;&#x8BA9;&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x5BF9;&#x5FC3;&#x7231;&#x7684;&#x7535;&#x5F71;&#x8FDB;&#x884C;&#x5206;&#x7C7B;&#x3002;&#x6211;&#x4EEC;&#x5148;&#x521B;&#x5EFA;&#x4E24;&#x4E2A;&#x65B0;&#x7684; schemas&#xFF1A;<code>Movie</code> &#x548C; <code>Character</code>&#x3002;&#x6211;&#x4EEC;&#x5148;&#x5B9E;&#x73B0;&#x8FD9;&#x4E24;&#x4E2A; schemas &#x4E4B;&#x95F4;&#x7684;&#x201C;&#x5C5E;&#x4E8E;/&#x4E00;&#x5BF9;&#x591A;&#x201D;&#x7684;&#x5173;&#x7CFB;&#xFF1A;&#x4E00;&#x90E8;&#x7535;&#x5F71;&#x62E5;&#x6709;&#x591A;&#x4E2A;&#x89D2;&#x8272;&#xFF0C;&#x548C;&#x4E00;&#x4E2A;&#x89D2;&#x8272;&#x5C5E;&#x4E8E;&#x4E00;&#x90E8;&#x7535;&#x5F71;&#x201C;&#x3002;</p>
<h4 id="&#x4E00;&#x5BF9;&#x591A;&#x7684;-migration">&#x201C;&#x4E00;&#x5BF9;&#x591A;&#x201D;&#x7684; Migration</h4>
<p>&#x8BA9;&#x6211;&#x4EEC;&#x5148;&#x521B;&#x5EFA; <code>Movie</code> &#x7684; migration&#xFF1A;</p>
<pre><code class="lang-console">mix ecto.gen.migration create_movies
</code></pre>
<p>&#x6253;&#x5F00;&#x65B0;&#x521B;&#x5EFA;&#x7684; migration &#x6587;&#x4EF6;&#xFF0C;&#x7136;&#x540E;&#x5B9A;&#x4E49; <code>change</code> &#x51FD;&#x6570;&#x6765;&#x521B;&#x5EFA; <code>movies</code> &#x8868;&#x5355;&#xFF1A;</p>
<pre><code class="lang-elixir"><span class="hljs-comment"># priv/repo/migrations/*_create_movies.exs</span>
<span class="hljs-class"><span class="hljs-keyword">defmodule</span> <span class="hljs-title">Example</span></span>.Repo.Migrations.CreateMovies <span class="hljs-keyword">do</span>
  <span class="hljs-keyword">use</span> Ecto.Migration

  <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">change</span></span> <span class="hljs-keyword">do</span>
    create table(<span class="hljs-symbol">:movies</span>) <span class="hljs-keyword">do</span>
      add <span class="hljs-symbol">:title</span>, <span class="hljs-symbol">:string</span>
      add <span class="hljs-symbol">:tagline</span>, <span class="hljs-symbol">:string</span>
    <span class="hljs-keyword">end</span>
  <span class="hljs-keyword">end</span>
<span class="hljs-keyword">end</span>
</code></pre>
<h4 id="&#x4E00;&#x5BF9;&#x591A;&#x7684;-schema">&#x201C;&#x4E00;&#x5BF9;&#x591A;&#x201D;&#x7684; Schema</h4>
<p>&#x7136;&#x540E;&#x6211;&#x4EEC;&#x6DFB;&#x52A0;&#x6307;&#x5B9A;&#x7535;&#x5F71;&#x548C;&#x89D2;&#x8272;&#x4E4B;&#x95F4;&#x7684;&#x201C;&#x4E00;&#x5BF9;&#x591A;&#x201D;&#x5173;&#x7CFB;&#x7684; schema&#x3002;</p>
<pre><code class="lang-elixir"><span class="hljs-comment"># lib/example/movie.ex</span>
<span class="hljs-class"><span class="hljs-keyword">defmodule</span> <span class="hljs-title">Example</span></span>.Movie <span class="hljs-keyword">do</span>
  <span class="hljs-keyword">use</span> Ecto.Schema

  schema <span class="hljs-string">&quot;movies&quot;</span> <span class="hljs-keyword">do</span>
    field <span class="hljs-symbol">:title</span>, <span class="hljs-symbol">:string</span>
    field <span class="hljs-symbol">:tagline</span>, <span class="hljs-symbol">:string</span>
    has_many <span class="hljs-symbol">:characters</span>, Example.Character
  <span class="hljs-keyword">end</span>
<span class="hljs-keyword">end</span>
</code></pre>
<p><code>has_many/3</code> &#x5B8F;&#x5E76;&#x4E0D;&#x4F1A;&#x5728;&#x6570;&#x636E;&#x5E93;&#x6DFB;&#x52A0;&#x4EFB;&#x4F55;&#x4E1C;&#x897F;&#x3002;&#x5B83;&#x53EA;&#x662F;&#x7528;&#x5916;&#x952E;&#x5173;&#x8054;&#x5230;&#x76F8;&#x5173;&#x7684; <code>characters</code> schema &#x4E0A;&#xFF0C;&#x4F7F;&#x5F97;&#x4E00;&#x90E8;&#x7535;&#x5F71;&#x53EF;&#x4EE5;&#x83B7;&#x53D6;&#x76F8;&#x5E94;&#x7684;&#x89D2;&#x8272;&#x3002;&#x8FD9;&#x5C31;&#x80FD;&#x8BA9;&#x6211;&#x4EEC;&#x901A;&#x8FC7;&#x8C03;&#x7528; <code>movie.characters</code> &#x6765;&#x83B7;&#x53D6;&#x76F8;&#x5E94;&#x7684;&#x6570;&#x636E;&#x3002;</p>
<h4 id="&#x5C5E;&#x4E8E;&#x7684;-migration">&#x201C;&#x5C5E;&#x4E8E;&#x201D;&#x7684; Migration</h4>
<p>&#x73B0;&#x5728;&#xFF0C;&#x6211;&#x4EEC;&#x5C31;&#x53EF;&#x4EE5;&#x6253;&#x9020; <code>Character</code> &#x7684; migration &#x548C; schema &#x4E86;&#x3002;&#x4E00;&#x4E2A;&#x89D2;&#x8272;&#x5C5E;&#x4E8E;&#x4E00;&#x90E8;&#x7535;&#x5F71;&#xFF0C;&#x6240;&#x4EE5;&#x6211;&#x4EEC;&#x8981;&#x76F8;&#x5E94;&#x7684; migration &#x548C; schema &#x6765;&#x5B9A;&#x4E49;&#x8FD9;&#x4E2A;&#x5173;&#x7CFB;&#x3002;</p>
<p>&#x9996;&#x5148;&#xFF0C;&#x6211;&#x4EEC;&#x521B;&#x5EFA; migration&#xFF1A;</p>
<pre><code class="lang-console">mix ecto.gen.migration create_characters
</code></pre>
<p>&#x4E3A;&#x4E86;&#x6307;&#x660E;&#x4E00;&#x4E2A;&#x89D2;&#x8272;&#x5C5E;&#x4E8E;&#x4E00;&#x90E8;&#x7535;&#x5F71;&#xFF0C;&#x6211;&#x4EEC;&#x9700;&#x8981; <code>characters</code> &#x8868;&#x6709;&#x4E00;&#x4E2A; <code>movie_id</code> &#x5B57;&#x6BB5;&#x3002;&#x6211;&#x4EEC;&#x5E0C;&#x671B;&#x8FD9;&#x4E2A;&#x5B57;&#x6BB5;&#x4F5C;&#x4E3A;&#x5916;&#x952E;&#x6765;&#x4F7F;&#x7528;&#x3002;&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x901A;&#x8FC7;&#x6DFB;&#x52A0;&#x4E0B;&#x9762;&#x4E00;&#x884C;&#x4EE3;&#x7801;&#x5230; <code>create table/1</code> &#x51FD;&#x6570;&#x6765;&#x5B9E;&#x73B0;&#xFF1A;</p>
<pre><code class="lang-elixir">add <span class="hljs-symbol">:movie_id</span>, references(<span class="hljs-symbol">:movies</span>)
</code></pre>
<p>&#x6240;&#x4EE5;&#xFF0C;&#x76F8;&#x5E94;&#x7684; migration &#x5E94;&#x8BE5;&#x662F;&#x8FD9;&#x4E2A;&#x6837;&#x5B50;&#xFF1A;</p>
<pre><code class="lang-elixir"><span class="hljs-comment"># priv/migrations/*_create_characters.exs</span>
<span class="hljs-class"><span class="hljs-keyword">defmodule</span> <span class="hljs-title">Example</span></span>.Repo.Migrations.CreateCharacters <span class="hljs-keyword">do</span>
  <span class="hljs-keyword">use</span> Ecto.Migration

  <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">change</span></span> <span class="hljs-keyword">do</span>
    create table(<span class="hljs-symbol">:characters</span>) <span class="hljs-keyword">do</span>
      add <span class="hljs-symbol">:name</span>, <span class="hljs-symbol">:string</span>
      add <span class="hljs-symbol">:movie_id</span>, references(<span class="hljs-symbol">:movies</span>)
    <span class="hljs-keyword">end</span>
  <span class="hljs-keyword">end</span>
<span class="hljs-keyword">end</span>
</code></pre>
<h4 id="&#x5C5E;&#x4E8E;&#x7684;-schema">&#x201C;&#x5C5E;&#x4E8E;&#x201D;&#x7684; Schema</h4>
<p>&#x6211;&#x4EEC;&#x7684; schema &#x4E5F;&#x8981;&#x76F8;&#x5E94;&#x7684;&#x5B9A;&#x4E49;&#x89D2;&#x8272;&#x201C;&#x5C5E;&#x4E8E;&#x201D;&#x5B83;&#x7684;&#x7535;&#x5F71;&#x7684;&#x5173;&#x7CFB;&#x3002;</p>
<pre><code class="lang-elixir"><span class="hljs-comment"># lib/example/character.ex</span>

<span class="hljs-class"><span class="hljs-keyword">defmodule</span> <span class="hljs-title">Example</span></span>.Character <span class="hljs-keyword">do</span>
  <span class="hljs-keyword">use</span> Ecto.Schema

  schema <span class="hljs-string">&quot;characters&quot;</span> <span class="hljs-keyword">do</span>
    field <span class="hljs-symbol">:name</span>, <span class="hljs-symbol">:string</span>
    belongs_to <span class="hljs-symbol">:movie</span>, Example.Movie
  <span class="hljs-keyword">end</span>
<span class="hljs-keyword">end</span>
</code></pre>
<p>&#x8BA9;&#x6211;&#x4EEC;&#x4ED4;&#x7EC6;&#x770B;&#x770B; <code>belongs_to/3</code> &#x8FD9;&#x4E2A;&#x5B8F;&#x4E3A;&#x6211;&#x4EEC;&#x505A;&#x4E86;&#x4EC0;&#x4E48;&#x3002;&#x548C;&#x5728; <code>characters</code> &#x8868;&#x6DFB;&#x52A0; <code>movie_id</code> &#x5B57;&#x6BB5;&#x4E0D;&#x540C;&#xFF0C;&#x8FD9;&#x4E2A;&#x5B8F; <em>&#x5E76;&#x4E0D;&#x4F1A;</em> &#x5F80;&#x6570;&#x636E;&#x5E93;&#x6DFB;&#x52A0;&#x4EFB;&#x4F55;&#x4E1C;&#x897F;&#x3002;&#x5B83; <em>&#x53EA;&#x662F;</em> &#x8BA9;&#x6211;&#x4EEC;&#x53EF;&#x4EE5; <em>&#x901A;&#x8FC7;</em> <code>characters</code> &#x6765;&#x8BBF;&#x95EE;&#x5173;&#x8054;&#x7684; <code>movies</code> schema&#x3002;&#x5B83;&#x5229;&#x7528; <code>characters</code> &#x4E0A;&#x9762;&#x7684; <code>movie_id</code> &#x5916;&#x952E;&#xFF0C;&#x53EF;&#x4F7F;&#x5F97;&#x89D2;&#x8272;&#x76F8;&#x5173;&#x7684;&#x7535;&#x5F71;&#x80FD;&#x5728;&#x67E5;&#x8BE2;&#x7684;&#x540C;&#x65F6;&#x53EF;&#x8BBF;&#x95EE;&#x3002;&#x6548;&#x679C;&#x5C31;&#x662F;&#x5141;&#x8BB8;&#x6211;&#x4EEC;&#x8C03;&#x7528; <code>character.movie</code>&#x3002;</p>
<p>&#x73B0;&#x5728;&#x6211;&#x4EEC;&#x5C31;&#x53EF;&#x4EE5;&#x8FD0;&#x884C; migration &#x547D;&#x4EE4;&#x4E86;&#xFF1A;</p>
<pre><code class="lang-console">mix ecto.migrate
</code></pre>
<h3 id="&#x5C5E;&#x4E8E;&#x4E00;&#x5BF9;&#x4E00;">&#x5C5E;&#x4E8E;/&#x4E00;&#x5BF9;&#x4E00;</h3>
<p>&#x6BD4;&#x5982;&#x8BF4;&#xFF0C;&#x4E00;&#x90E8;&#x7535;&#x5F71;&#x6709;&#x4E00;&#x4E2A;&#x5206;&#x9500;&#x5546;&#x3002;&#x4F8B;&#x5982;&#xFF0C;Netflix &#x662F;&#x5B83;&#x4EEC;&#x7684;&#x539F;&#x521B;&#x7535;&#x5F71;&#x201C;Bright&#x201D;&#x7684;&#x5206;&#x9500;&#x5546;&#x3002;</p>
<p>&#x6211;&#x4EEC;&#x4E0B;&#x9762;&#x6765;&#x5B9A;&#x4E49; <code>Distributor</code> migration &#x548C; schema &#x4EE5;&#x53CA;&#x201C;&#x4E00;&#x5BF9;&#x4E00;&#x201D;&#x7684;&#x5173;&#x7CFB;&#x3002;&#x9996;&#x5148;&#xFF0C;&#x8BA9;&#x6211;&#x4EEC;&#x6765;&#x751F;&#x6210; migration&#xFF1A;</p>
<pre><code class="lang-console">mix ecto.gen.migration create_distributors
</code></pre>
<p>&#x8FD9;&#x4E2A; migration &#x9700;&#x8981;&#x6DFB;&#x52A0;&#x4E00;&#x4E2A;&#x5916;&#x952E; <code>movie_id</code> &#x5230; <code>distributors</code> &#x8868;&#x91CC;&#x9762;&#xFF1A;</p>
<pre><code class="lang-elixir"><span class="hljs-comment"># priv/repo/migrations/*_create_distributors.exs</span>

<span class="hljs-class"><span class="hljs-keyword">defmodule</span> <span class="hljs-title">Example</span></span>.Repo.Migrations.CreateDistributors <span class="hljs-keyword">do</span>
  <span class="hljs-keyword">use</span> Ecto.Migration

  <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">change</span></span> <span class="hljs-keyword">do</span>
    create table(<span class="hljs-symbol">:distributors</span>) <span class="hljs-keyword">do</span>
      add <span class="hljs-symbol">:name</span>, <span class="hljs-symbol">:string</span>
      add <span class="hljs-symbol">:movie_id</span>, references(<span class="hljs-symbol">:movies</span>)
    <span class="hljs-keyword">end</span>
  <span class="hljs-keyword">end</span>
<span class="hljs-keyword">end</span>
</code></pre>
<p>&#x7136;&#x540E; <code>Distributor</code> schema &#x5E94;&#x8BE5;&#x4F7F;&#x7528; <code>belongs_to/3</code> &#x5B8F;&#x6765;&#x4F7F;&#x5F97;&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x8C03;&#x7528; <code>distributor.movie</code> &#x6765;&#x901A;&#x8FC7;&#x5916;&#x952E;&#x67E5;&#x627E;&#x76F8;&#x5E94;&#x7684;&#x5206;&#x9500;&#x5546;&#x3002;</p>
<pre><code class="lang-elixir"><span class="hljs-comment"># lib/example/distributor.ex</span>

<span class="hljs-class"><span class="hljs-keyword">defmodule</span> <span class="hljs-title">Example</span></span>.Distributor <span class="hljs-keyword">do</span>
  <span class="hljs-keyword">use</span> Ecto.Schema

  schema <span class="hljs-string">&quot;distributors&quot;</span> <span class="hljs-keyword">do</span>
    field <span class="hljs-symbol">:name</span>, <span class="hljs-symbol">:string</span>
    belongs_to <span class="hljs-symbol">:movie</span>, Example.Movie
  <span class="hljs-keyword">end</span>
<span class="hljs-keyword">end</span>
</code></pre>
<p>&#x63A5;&#x7740;&#xFF0C;&#x6211;&#x4EEC;&#x5C31;&#x53EF;&#x4EE5;&#x628A;&#x201C;&#x4E00;&#x5BF9;&#x4E00;&#x201D;&#x5173;&#x7CFB;&#x6DFB;&#x52A0;&#x5230; <code>Movie</code> schema&#xFF1A;</p>
<pre><code class="lang-elixir"><span class="hljs-comment"># lib/example/movie.ex</span>

<span class="hljs-class"><span class="hljs-keyword">defmodule</span> <span class="hljs-title">Example</span></span>.Movie <span class="hljs-keyword">do</span>
  <span class="hljs-keyword">use</span> Ecto.Schema

  schema <span class="hljs-string">&quot;movies&quot;</span> <span class="hljs-keyword">do</span>
    field <span class="hljs-symbol">:title</span>, <span class="hljs-symbol">:string</span>
    field <span class="hljs-symbol">:tagline</span>, <span class="hljs-symbol">:string</span>
    has_many <span class="hljs-symbol">:characters</span>, Example.Character
    has_one <span class="hljs-symbol">:distributor</span>, Example.Distributor <span class="hljs-comment"># I&apos;m new!</span>
  <span class="hljs-keyword">end</span>
<span class="hljs-keyword">end</span>
</code></pre>
<p><code>has_one/3</code> &#x5B8F;&#x548C; <code>has_many/3</code> &#x5B8F;&#x4E00;&#x6837;&#x3002;&#x5B83;&#x4E0D;&#x4F1A;&#x5728;&#x6570;&#x636E;&#x5E93;&#x6DFB;&#x52A0;&#x4EFB;&#x4F55;&#x4E1C;&#x897F;&#xFF0C;&#x5B83; <em>&#x53EA;</em> &#x4F7F;&#x7528;&#x4E86; schema &#x4E2D;&#x76F8;&#x5E94;&#x7684;&#x5916;&#x952E;&#x6765;&#x67E5;&#x627E;&#x7535;&#x5F71;&#x7684;&#x5206;&#x9500;&#x5546;&#x3002;&#x8FD9;&#x5C31;&#x4F7F;&#x5F97;&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x8C03;&#x7528; <code>movie.distributor</code> &#x6765;&#x83B7;&#x53D6;&#x6570;&#x636E;&#x3002;</p>
<p>&#x6211;&#x4EEC;&#x73B0;&#x5728;&#x5C31;&#x53EF;&#x4EE5;&#x8FD0;&#x884C; migration &#x4E86;&#xFF1A;</p>
<pre><code class="lang-console">mix ecto.migrate
</code></pre>
<h3 id="&#x591A;&#x5BF9;&#x591A;">&#x591A;&#x5BF9;&#x591A;</h3>
<p>&#x4E00;&#x90E8;&#x7535;&#x5F71;&#x53EF;&#x4EE5;&#x6709;&#x591A;&#x4E2A;&#x6F14;&#x5458;&#xFF0C;&#x4E00;&#x4E2A;&#x6F14;&#x5458;&#x53EF;&#x4EE5;&#x51FA;&#x6F14;&#x591A;&#x90E8;&#x7535;&#x5F71;&#x3002;&#x6211;&#x4EEC;&#x5EFA;&#x7ACB;&#x4E00;&#x4E2A;&#x5173;&#x8054;&#x8868;&#x6765;&#x628A; movies <em>&#x548C;</em> actors &#x4E24;&#x4E2A;&#x8868;&#x5173;&#x8054;&#x8D77;&#x6765;&#x5B9E;&#x73B0;&#x8FD9;&#x4E2A;&#x5173;&#x7CFB;&#x3002;</p>
<p>&#x9996;&#x5148;&#xFF0C;&#x8BA9;&#x6211;&#x4EEC;&#x751F;&#x6210; <code>Actors</code> migration&#xFF1A;</p>
<pre><code class="lang-console">mix ecto.gen.migration create_actors
</code></pre>
<p>&#x5B9A;&#x4E49; migration &#x5185;&#x5BB9;&#xFF1A;</p>
<pre><code class="lang-elixir"><span class="hljs-comment"># priv/migrations/*_create_actors.ex</span>

<span class="hljs-class"><span class="hljs-keyword">defmodule</span> <span class="hljs-title">Example</span></span>.Repo.Migrations.Actors <span class="hljs-keyword">do</span>
  <span class="hljs-keyword">use</span> Ecto.Migration

  <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">change</span></span> <span class="hljs-keyword">do</span>
    create table(<span class="hljs-symbol">:actors</span>) <span class="hljs-keyword">do</span>
      add <span class="hljs-symbol">:name</span>, <span class="hljs-symbol">:string</span>
    <span class="hljs-keyword">end</span>
  <span class="hljs-keyword">end</span>
<span class="hljs-keyword">end</span>
</code></pre>
<p>&#x8BA9;&#x6211;&#x4EEC;&#x6765;&#x751F;&#x6210;&#x5173;&#x8054;&#x8868;&#x7684; migration&#xFF1A;</p>
<pre><code class="lang-console">mix ecto.gen.migration create_movies_actors
</code></pre>
<p>&#x6211;&#x4EEC;&#x5C06;&#x5B9A;&#x4E49;&#x7684; migration &#x4F1A;&#x62E5;&#x6709;&#x4E24;&#x4E2A;&#x5916;&#x952E;&#x3002;&#x6211;&#x4EEC;&#x8FD8;&#x8981;&#x6DFB;&#x52A0;&#x4E00;&#x4E2A;&#x552F;&#x4E00;&#x7D22;&#x5F15;&#x6765;&#x52A0;&#x5F3A;&#x6F14;&#x5458;&#x548C;&#x7535;&#x5F71;&#x4E4B;&#x95F4;&#x7684;&#x552F;&#x4E00;&#x6027;&#xFF1A;</p>
<pre><code class="lang-elixir"><span class="hljs-comment"># priv/migrations/*_create_movies_actors.ex</span>

<span class="hljs-class"><span class="hljs-keyword">defmodule</span> <span class="hljs-title">Example</span></span>.Repo.Migrations.CreateMoviesActors <span class="hljs-keyword">do</span>
  <span class="hljs-keyword">use</span> Ecto.Migration

  <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">change</span></span> <span class="hljs-keyword">do</span>
    create table(<span class="hljs-symbol">:movies_actors</span>) <span class="hljs-keyword">do</span>
      add <span class="hljs-symbol">:movie_id</span>, references(<span class="hljs-symbol">:movies</span>)
      add <span class="hljs-symbol">:actor_id</span>, references(<span class="hljs-symbol">:actors</span>)
    <span class="hljs-keyword">end</span>

    create unique_index(<span class="hljs-symbol">:movies_actors</span>, [<span class="hljs-symbol">:movie_id</span>, <span class="hljs-symbol">:actor_id</span>])
  <span class="hljs-keyword">end</span>
<span class="hljs-keyword">end</span>
</code></pre>
<p>&#x63A5;&#x7740;&#xFF0C;&#x6DFB;&#x52A0;&#x4E00;&#x4E2A; <code>many_to_many</code> &#x5B8F;&#x5230; <code>Movie</code> schema&#xFF1A;</p>
<pre><code class="lang-elixir"><span class="hljs-comment"># lib/example/movie.ex</span>

<span class="hljs-class"><span class="hljs-keyword">defmodule</span> <span class="hljs-title">Example</span></span>.Movie <span class="hljs-keyword">do</span>
  <span class="hljs-keyword">use</span> Ecto.Schema

  schema <span class="hljs-string">&quot;movies&quot;</span> <span class="hljs-keyword">do</span>
    field <span class="hljs-symbol">:title</span>, <span class="hljs-symbol">:string</span>
    field <span class="hljs-symbol">:tagline</span>, <span class="hljs-symbol">:string</span>
    has_many <span class="hljs-symbol">:characters</span>, Example.Character
    has_one <span class="hljs-symbol">:distributor</span>, Example.Distributor
    many_to_many <span class="hljs-symbol">:actors</span>, Example.Actor, <span class="hljs-symbol">join_through:</span> <span class="hljs-string">&quot;movies_actors&quot;</span> <span class="hljs-comment"># I&apos;m new!</span>
  <span class="hljs-keyword">end</span>
<span class="hljs-keyword">end</span>
</code></pre>
<p>&#x6700;&#x540E;&#xFF0C;&#x4F7F;&#x7528;&#x540C;&#x6837;&#x7684; <code>many_to_many</code> &#x5B8F;&#x6765;&#x5B9A;&#x4E49;&#x6211;&#x4EEC;&#x7684; <code>Actor</code> schema&#x3002;</p>
<pre><code class="lang-elixir"><span class="hljs-comment"># lib/example/actor.ex</span>

<span class="hljs-class"><span class="hljs-keyword">defmodule</span> <span class="hljs-title">Example</span></span>.Actor <span class="hljs-keyword">do</span>
  <span class="hljs-keyword">use</span> Ecto.Schema

  schema <span class="hljs-string">&quot;actors&quot;</span> <span class="hljs-keyword">do</span>
    field <span class="hljs-symbol">:name</span>, <span class="hljs-symbol">:string</span>
    many_to_many <span class="hljs-symbol">:movies</span>, Example.Movie, <span class="hljs-symbol">join_through:</span> <span class="hljs-string">&quot;movies_actors&quot;</span>
  <span class="hljs-keyword">end</span>
<span class="hljs-keyword">end</span>
</code></pre>
<p>&#x53EF;&#x4EE5;&#x8FD0;&#x884C; migration &#x4E86;&#xFF1A;</p>
<pre><code class="lang-console">mix ecto.migrate
</code></pre>
<h2 id="&#x4FDD;&#x5B58;&#x5173;&#x8054;&#x6570;&#x636E;">&#x4FDD;&#x5B58;&#x5173;&#x8054;&#x6570;&#x636E;</h2>
<p>&#x6211;&#x4EEC;&#x4FDD;&#x5B58;&#x6570;&#x636E;&#x53CA;&#x5176;&#x5173;&#x8054;&#x5173;&#x7CFB;&#x7684;&#x65B9;&#x5F0F;&#xFF0C;&#x4F9D;&#x8D56;&#x4E8E;&#x6570;&#x636E;&#x4E4B;&#x95F4;&#x7684;&#x5173;&#x7CFB;&#x7684;&#x7279;&#x6027;&#x3002;&#x6211;&#x4EEC;&#x5148;&#x6765;&#x770B;&#x770B;&#x201C;&#x5C5E;&#x4E8E;/&#x4E00;&#x5BF9;&#x591A;&#x201D;&#x7684;&#x5173;&#x7CFB;&#x3002;</p>
<h3 id="&#x5C5E;&#x4E8E;">&#x201C;&#x5C5E;&#x4E8E;&#x201D;</h3>
<h4 id="&#x901A;&#x8FC7;-ectobuildassoc3-&#x6765;&#x4FDD;&#x5B58;">&#x901A;&#x8FC7; <code>Ecto.build_assoc/3</code> &#x6765;&#x4FDD;&#x5B58;</h4>
<p>&#x5BF9;&#x4E8E;&quot;&#x5C5E;&#x4E8E;&quot;&#x8FD9;&#x79CD;&#x5173;&#x7CFB;&#xFF0C;&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x901A;&#x8FC7; Ecto &#x7684; <code>build_assoc/3</code> &#x51FD;&#x6570;&#x6765;&#x5904;&#x7406;&#x3002;</p>
<p><a href="https://hexdocs.pm/ecto/Ecto.html#build_assoc/3" target="_blank"><code>build_assoc/3</code></a> &#x63A5;&#x6536;&#x4E09;&#x4E2A;&#x53C2;&#x6570;&#xFF1A;</p>
<ul>
<li>&#x9700;&#x8981;&#x4FDD;&#x5B58;&#x7684;&#x6570;&#x636E;&#x7684;&#x7ED3;&#x6784;&#x4F53;  </li>
<li>&#x5173;&#x7CFB;&#x7684;&#x540D;&#x5B57;  </li>
<li>&#x5176;&#x5B83;&#x9700;&#x8981;&#x4FDD;&#x5B58;&#xFF0C;&#x5E76;&#x8D4B;&#x503C;&#x7684;&#x5173;&#x7CFB;&#x8BB0;&#x5F55;&#x5C5E;&#x6027;  </li>
</ul>
<p>&#x6211;&#x4EEC;&#x6765;&#x4FDD;&#x5B58;&#x4E00;&#x4E2A;&#x7535;&#x5F71;&#x548C;&#x76F8;&#x5173;&#x7684;&#x89D2;&#x8272;&#xFF1A;</p>
<p>&#x9996;&#x5148;&#xFF0C;&#x6211;&#x4EEC;&#x8981;&#x521B;&#x5EFA;&#x4E00;&#x4E2A;&#x7535;&#x5F71;&#x8BB0;&#x5F55;&#xFF1A;</p>
<pre><code class="lang-elixir">iex&gt; <span class="hljs-keyword">alias</span> Example.{Movie, Character, Repo}
iex&gt; movie = %Movie{<span class="hljs-symbol">title:</span> <span class="hljs-string">&quot;Ready Player One&quot;</span>, <span class="hljs-symbol">tagline:</span> <span class="hljs-string">&quot;Something about video games&quot;</span>}

%Example.Movie{
  <span class="hljs-symbol">__meta__:</span> %Ecto.Schema.Metadata&lt;<span class="hljs-symbol">:built</span>, <span class="hljs-string">&quot;movies&quot;</span>&gt;,
  <span class="hljs-symbol">actors:</span> %Ecto.Association.NotLoaded&lt;association <span class="hljs-symbol">:actors</span> is <span class="hljs-keyword">not</span> loaded&gt;,
  <span class="hljs-symbol">characters:</span> %Ecto.Association.NotLoaded&lt;association <span class="hljs-symbol">:characters</span> is <span class="hljs-keyword">not</span> loaded&gt;,
  <span class="hljs-symbol">distributor:</span> %Ecto.Association.NotLoaded&lt;association <span class="hljs-symbol">:distributor</span> is <span class="hljs-keyword">not</span> loaded&gt;,
  <span class="hljs-symbol">id:</span> <span class="hljs-keyword">nil</span>,
  <span class="hljs-symbol">tagline:</span> <span class="hljs-string">&quot;Something about video games&quot;</span>,
  <span class="hljs-symbol">title:</span> <span class="hljs-string">&quot;Ready Player One&quot;</span>
}

iex&gt; movie = Repo.insert!(movie)
</code></pre>
<p>&#x73B0;&#x5728;&#x6211;&#x4EEC;&#x8981;&#x521B;&#x5EFA;&#x76F8;&#x5173;&#x7684;&#x89D2;&#x8272;&#x548C;&#x4FDD;&#x5B58;&#x5230;&#x6570;&#x636E;&#x5E93;&#x91CC;&#xFF1A;</p>
<pre><code class="lang-elixir">character = Ecto.build_assoc(movie, <span class="hljs-symbol">:characters</span>, %{<span class="hljs-symbol">name:</span> <span class="hljs-string">&quot;Wade Watts&quot;</span>})
%Example.Character{
  <span class="hljs-symbol">__meta__:</span> %Ecto.Schema.Metadata&lt;<span class="hljs-symbol">:built</span>, <span class="hljs-string">&quot;characters&quot;</span>&gt;,
  <span class="hljs-symbol">id:</span> <span class="hljs-keyword">nil</span>,
  <span class="hljs-symbol">movie:</span> %Ecto.Association.NotLoaded&lt;association <span class="hljs-symbol">:movie</span> is <span class="hljs-keyword">not</span> loaded&gt;,
  <span class="hljs-symbol">movie_id:</span> <span class="hljs-number">1</span>,
  <span class="hljs-symbol">name:</span> <span class="hljs-string">&quot;Wade Watts&quot;</span>
}
Repo.insert!(character)
%Example.Character{
  <span class="hljs-symbol">__meta__:</span> %Ecto.Schema.Metadata&lt;<span class="hljs-symbol">:loaded</span>, <span class="hljs-string">&quot;characters&quot;</span>&gt;,
  <span class="hljs-symbol">id:</span> <span class="hljs-number">1</span>,
  <span class="hljs-symbol">movie:</span> %Ecto.Association.NotLoaded&lt;association <span class="hljs-symbol">:movie</span> is <span class="hljs-keyword">not</span> loaded&gt;,
  <span class="hljs-symbol">movie_id:</span> <span class="hljs-number">1</span>,
  <span class="hljs-symbol">name:</span> <span class="hljs-string">&quot;Wade Watts&quot;</span>
}
</code></pre>
<p>&#x8981;&#x6CE8;&#x610F;&#x7684;&#x662F;&#xFF0C;&#x56E0;&#x4E3A; <code>Movie</code> schema &#x4E2D;&#x7684; <code>has_many/3</code> &#x5B8F;&#x6307;&#x5B9A;&#x4E86;&#x4E00;&#x90E8;&#x7535;&#x5F71;&#x62E5;&#x6709;&#x591A;&#x4E2A; <code>:characters</code>&#xFF0C;&#x6211;&#x4EEC;&#x901A;&#x8FC7;&#x7B2C;&#x4E8C;&#x4E2A;&#x53C2;&#x6570;&#x4F20;&#x5230; <code>build_assoc/3</code> &#x7684;&#x5173;&#x7CFB;&#x7684;&#x540D;&#x5B57;&#xFF0C;&#x5C31;&#x662F; <code>:characters</code>&#x3002;&#x8FD9;&#x6837;&#xFF0C;&#x6211;&#x4EEC;&#x5C31;&#x521B;&#x5EFA;&#x4E86;&#x4E00;&#x4E2A;&#x628A;&#x76F8;&#x5E94;&#x7684;&#x7535;&#x5F71; ID &#x8BBE;&#x7F6E;&#x5230;&#x4E86; <code>movie_id</code> &#x7684;&#x89D2;&#x8272;&#x3002;</p>
<p>&#x4E3A;&#x4E86;&#x4F7F;&#x7528; <code>build_assoc/3</code> &#x6765;&#x4FDD;&#x5B58;&#x7535;&#x5F71;&#x76F8;&#x5E94;&#x7684;&#x5206;&#x9500;&#x5546;&#xFF0C;&#x6211;&#x4EEC;&#x7528;&#x540C;&#x6837;&#x7684;&#x65B9;&#x5F0F;&#xFF0C;&#x4F20;&#x5165;&#x7535;&#x5F71;&#x548C;&#x5206;&#x9500;&#x5546;&#x7684;&#x5173;&#x7CFB; <em>&#x540D;&#x79F0;</em> &#x4F5C;&#x4E3A; <code>build_assoc/3</code> &#x7684;&#x7B2C;&#x4E8C;&#x4E2A;&#x53C2;&#x6570;&#xFF1A;</p>
<pre><code class="lang-elixir">iex&gt; distributor = Ecto.build_assoc(movie, <span class="hljs-symbol">:distributor</span>, %{<span class="hljs-symbol">name:</span> <span class="hljs-string">&quot;Netflix&quot;</span>})       
%Example.Distributor{
  <span class="hljs-symbol">__meta__:</span> %Ecto.Schema.Metadata&lt;<span class="hljs-symbol">:built</span>, <span class="hljs-string">&quot;distributors&quot;</span>&gt;,
  <span class="hljs-symbol">id:</span> <span class="hljs-keyword">nil</span>,
  <span class="hljs-symbol">movie:</span> %Ecto.Association.NotLoaded&lt;association <span class="hljs-symbol">:movie</span> is <span class="hljs-keyword">not</span> loaded&gt;,
  <span class="hljs-symbol">movie_id:</span> <span class="hljs-number">1</span>,
  <span class="hljs-symbol">name:</span> <span class="hljs-string">&quot;Netflix&quot;</span>
}
iex&gt; Repo.insert!(distributor)
%Example.Distributor{
  <span class="hljs-symbol">__meta__:</span> %Ecto.Schema.Metadata&lt;<span class="hljs-symbol">:loaded</span>, <span class="hljs-string">&quot;distributors&quot;</span>&gt;,
  <span class="hljs-symbol">id:</span> <span class="hljs-number">1</span>,
  <span class="hljs-symbol">movie:</span> %Ecto.Association.NotLoaded&lt;association <span class="hljs-symbol">:movie</span> is <span class="hljs-keyword">not</span> loaded&gt;,
  <span class="hljs-symbol">movie_id:</span> <span class="hljs-number">1</span>,
  <span class="hljs-symbol">name:</span> <span class="hljs-string">&quot;Netflix&quot;</span>
}
</code></pre>
<h3 id="&#x591A;&#x5BF9;&#x591A;">&#x591A;&#x5BF9;&#x591A;</h3>
<h4 id="&#x901A;&#x8FC7;-ectochangesetputassoc4-&#x6765;&#x4FDD;&#x5B58;">&#x901A;&#x8FC7; <code>Ecto.Changeset.put_assoc/4</code> &#x6765;&#x4FDD;&#x5B58;</h4>
<p><code>build_assoc/3</code> &#x7684;&#x505A;&#x6CD5;&#x662F;&#x4E0D;&#x80FD;&#x7528;&#x5728;&#x591A;&#x5BF9;&#x591A;&#x5173;&#x7CFB;&#x7684;&#x5904;&#x7406;&#x4E0A;&#x9762;&#x7684;&#x3002;&#x56E0;&#x4E3A; movie &#x6216;&#x8005; actor &#x8868;&#x672C;&#x8EAB;&#x90FD;&#x4E0D;&#x5305;&#x542B;&#x76F8;&#x5E94;&#x7684;&#x5916;&#x952E;&#x3002;&#x6211;&#x4EEC;&#x9700;&#x8981;&#x4F7F;&#x7528; Ecto Changesets &#x548C; <code>put_assoc/4</code> &#x51FD;&#x6570;&#x6765;&#x5904;&#x7406;&#x3002;</p>
<p>&#x5047;&#x5B9A;&#x6211;&#x4EEC;&#x5DF2;&#x7ECF;&#x6709;&#x4E86;&#x76F8;&#x5E94;&#x7684; movie &#x8BB0;&#x5F55;&#xFF0C;&#x73B0;&#x5728;&#x6211;&#x4EEC;&#x6765;&#x521B;&#x5EFA; actor &#x8BB0;&#x5F55;&#xFF1A;</p>
<pre><code class="lang-elixir">iex&gt; <span class="hljs-keyword">alias</span> Example.Actor
iex&gt; actor = %Actor{<span class="hljs-symbol">name:</span> <span class="hljs-string">&quot;Tyler Sheridan&quot;</span>}
%Example.Actor{
  <span class="hljs-symbol">__meta__:</span> %Ecto.Schema.Metadata&lt;<span class="hljs-symbol">:built</span>, <span class="hljs-string">&quot;actors&quot;</span>&gt;,
  <span class="hljs-symbol">id:</span> <span class="hljs-keyword">nil</span>,
  <span class="hljs-symbol">movies:</span> %Ecto.Association.NotLoaded&lt;association <span class="hljs-symbol">:movies</span> is <span class="hljs-keyword">not</span> loaded&gt;,
  <span class="hljs-symbol">name:</span> <span class="hljs-string">&quot;Tyler Sheridan&quot;</span>
}
iex&gt; actor = Repo.insert!(actor)
%Example.Actor{
  <span class="hljs-symbol">__meta__:</span> %Ecto.Schema.Metadata&lt;<span class="hljs-symbol">:loaded</span>, <span class="hljs-string">&quot;actors&quot;</span>&gt;,
  <span class="hljs-symbol">id:</span> <span class="hljs-number">1</span>,
  <span class="hljs-symbol">movies:</span> %Ecto.Association.NotLoaded&lt;association <span class="hljs-symbol">:movies</span> is <span class="hljs-keyword">not</span> loaded&gt;,
  <span class="hljs-symbol">name:</span> <span class="hljs-string">&quot;Tyler Sheridan&quot;</span>
}
</code></pre>
<p>&#x73B0;&#x5728;&#x6211;&#x4EEC;&#x5DF2;&#x7ECF;&#x4E3A;&#x901A;&#x8FC7;&#x5173;&#x8054;&#x8868;&#x6765;&#x5173;&#x8054;&#x7535;&#x5F71;&#x548C;&#x89D2;&#x8272;&#x505A;&#x597D;&#x51C6;&#x5907;&#x4E86;&#x3002;</p>
<p>&#x9996;&#x5148;&#xFF0C;&#x4E3A;&#x4E86;&#x521B;&#x5EFA; Changesets&#xFF0C;&#x6211;&#x4EEC;&#x9700;&#x8981;&#x786E;&#x4FDD; <code>movie</code> &#x8BB0;&#x5F55;&#x5DF2;&#x7ECF;&#x9884;&#x5148;&#x52A0;&#x8F7D;&#x4E86;&#x5173;&#x8054;&#x7684; schemas&#x3002;&#x5F88;&#x5FEB;&#x6211;&#x4EEC;&#x5C31;&#x4F1A;&#x8FDB;&#x4E00;&#x6B65;&#x89E3;&#x91CA;&#x9884;&#x52A0;&#x8F7D;&#x6570;&#x636E;&#x3002;&#x73B0;&#x5728;&#xFF0C;&#x6211;&#x4EEC;&#x53EA;&#x8981;&#x77E5;&#x9053;&#x4EE5;&#x4E0B;&#x4EE3;&#x7801;&#x80FD;&#x591F;&#x8FD9;&#x4E48;&#x505A;&#x5C31;&#x884C;&#x4E86;&#xFF1A;</p>
<pre><code class="lang-elixir">iex&gt; movie = Repo.preload(movie, [<span class="hljs-symbol">:distributor</span>, <span class="hljs-symbol">:characters</span>, <span class="hljs-symbol">:actors</span>])
%Example.Movie{
  <span class="hljs-symbol">__meta__:</span> %Ecto.Schema.Metadata&lt;<span class="hljs-symbol">:loaded</span>, <span class="hljs-string">&quot;movies&quot;</span>&gt;,
  <span class="hljs-symbol">actors:</span> [],
  <span class="hljs-symbol">characters:</span> [],
  <span class="hljs-symbol">distributor:</span> <span class="hljs-keyword">nil</span>,
  <span class="hljs-symbol">id:</span> <span class="hljs-number">1</span>,
  <span class="hljs-symbol">tagline:</span> <span class="hljs-string">&quot;Something about video games&quot;</span>,
  <span class="hljs-symbol">title:</span> <span class="hljs-string">&quot;Ready Player One&quot;</span>
}
</code></pre>
<p>&#x7136;&#x540E;&#xFF0C;&#x6211;&#x4EEC;&#x521B;&#x5EFA;&#x4E00;&#x4E2A;&#x7535;&#x5F71;&#x8BB0;&#x5F55;&#x7684; changeset&#xFF1A;</p>
<pre><code class="lang-elixir">iex&gt; movie_changeset = Ecto.Changeset.change(movie)                                                    
%Ecto.Changeset&lt;<span class="hljs-symbol">action:</span> <span class="hljs-keyword">nil</span>, <span class="hljs-symbol">changes:</span> %{}, <span class="hljs-symbol">errors:</span> [], <span class="hljs-symbol">data:</span> <span class="hljs-comment">#Example.Movie&lt;&gt;,</span>
 <span class="hljs-symbol">valid?:</span> <span class="hljs-keyword">true</span>&gt;
</code></pre>
<p>&#x73B0;&#x5728;&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x628A; changeset &#x4F5C;&#x4E3A;&#x7B2C;&#x4E00;&#x4E2A;&#x53C2;&#x6570;&#x4F20;&#x5165; <a href="https://hexdocs.pm/ecto/Ecto.Changeset.html#put_assoc/4" target="_blank"><code>Ecto.Changeset.put_assoc/4</code></a>&#xFF1A;</p>
<pre><code class="lang-elixir">iex&gt; movie_actors_changeset = movie_changeset |&gt; Ecto.Changeset.put_assoc(<span class="hljs-symbol">:actors</span>, [actor])
%Ecto.Changeset&lt;
  <span class="hljs-symbol">action:</span> <span class="hljs-keyword">nil</span>,
  <span class="hljs-symbol">changes:</span> %{
    <span class="hljs-symbol">actors:</span> [
      %Ecto.Changeset&lt;<span class="hljs-symbol">action:</span> <span class="hljs-symbol">:update</span>, <span class="hljs-symbol">changes:</span> %{}, <span class="hljs-symbol">errors:</span> [],
       <span class="hljs-symbol">data:</span> <span class="hljs-comment">#Example.Actor&lt;&gt;, valid?: true&gt;</span>
    ]
  },
  <span class="hljs-symbol">errors:</span> [],
  <span class="hljs-symbol">data:</span> <span class="hljs-comment">#Example.Movie&lt;&gt;,</span>
  <span class="hljs-symbol">valid?:</span> <span class="hljs-keyword">true</span>
&gt;
</code></pre>
<p>&#x6211;&#x4EEC;&#x8FD9;&#x6837;&#x5C31;&#x5F97;&#x5230;&#x4E86;&#x4E00;&#x4E2A; <em>&#x65B0;&#x7684;</em> changeset&#x3002;&#x5B83;&#x4EE3;&#x8868;&#x4E86;&#x8FD9;&#x4E2A;&#x53D8;&#x66F4;&#xFF1A;&#x628A;&#x89D2;&#x8272;&#x52A0;&#x5165;&#x5230;&#x6307;&#x5B9A; movie &#x8BB0;&#x5F55;&#x7684;&#x89D2;&#x8272;&#x5217;&#x8868;&#x3002;</p>
<p>&#x6700;&#x540E;&#xFF0C;&#x6211;&#x4EEC;&#x901A;&#x8FC7;&#x8FD9;&#x4E2A; changeset &#x6765;&#x66F4;&#x65B0;&#x6307;&#x5B9A;&#x7684; movie &#x548C; actor &#x8BB0;&#x5F55;&#xFF1A;</p>
<pre><code class="lang-elixir">iex&gt; Repo.update!(movie_actors_changeset)
%Example.Movie{
  <span class="hljs-symbol">__meta__:</span> %Ecto.Schema.Metadata&lt;<span class="hljs-symbol">:loaded</span>, <span class="hljs-string">&quot;movies&quot;</span>&gt;,
  <span class="hljs-symbol">actors:</span> [
    %Example.Actor{
      <span class="hljs-symbol">__meta__:</span> %Ecto.Schema.Metadata&lt;<span class="hljs-symbol">:loaded</span>, <span class="hljs-string">&quot;actors&quot;</span>&gt;,
      <span class="hljs-symbol">id:</span> <span class="hljs-number">1</span>,
      <span class="hljs-symbol">movies:</span> %Ecto.Association.NotLoaded&lt;association <span class="hljs-symbol">:movies</span> is <span class="hljs-keyword">not</span> loaded&gt;,
      <span class="hljs-symbol">name:</span> <span class="hljs-string">&quot;Bob&quot;</span>
    }
  ],
  <span class="hljs-symbol">characters:</span> [],
  <span class="hljs-symbol">distributor:</span> <span class="hljs-keyword">nil</span>,
  <span class="hljs-symbol">id:</span> <span class="hljs-number">1</span>,
  <span class="hljs-symbol">tagline:</span> <span class="hljs-string">&quot;Something about video games&quot;</span>,
  <span class="hljs-symbol">title:</span> <span class="hljs-string">&quot;Ready Player One&quot;</span>
}
</code></pre>
<p>&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x53D1;&#x73B0;&#xFF0C;&#x8FD9;&#x4F7F;&#x5F97;&#x6211;&#x4EEC;&#x7684; movie &#x8BB0;&#x5F55;&#x5305;&#x542B;&#x4E86;&#x5173;&#x8054;&#x4E0A;&#x7684;&#x65B0;&#x7684; actor &#x6570;&#x636E;&#xFF0C;&#x5E76;&#x9884;&#x52A0;&#x8F7D;&#x7684; <code>movie.actors</code> &#x91CC;&#x9762;&#x3002;</p>
<p>&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x4F7F;&#x7528;&#x540C;&#x6837;&#x7684;&#x65B9;&#x5F0F;&#x6765;&#x521B;&#x5EFA;&#x4E00;&#x4E2A;&#x65B0;&#x7684;&#x89D2;&#x8272;&#xFF0C;&#x5173;&#x8054;&#x5230;&#x7535;&#x5F71;&#x91CC;&#x9762;&#x3002;&#x4E0E;&#x5176;&#x4F20;&#x5165;&#x4E00;&#x4E2A; <em>&#x4FDD;&#x5B58;&#x8FC7;&#x7684;</em> &#x89D2;&#x8272;&#x7ED3;&#x6784;&#x4F53;&#x5230; <code>put_assoc/4</code> &#x91CC;&#xFF0C;&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x4F20;&#x5165;&#x4E00;&#x4E2A;&#x60F3;&#x521B;&#x5EFA;&#x7684;&#x65B0;&#x89D2;&#x8272;&#x7ED3;&#x6784;&#x4F53;&#x5C31;&#x884C;&#x4E86;&#xFF1A;</p>
<pre><code class="lang-elixir">iex&gt; changeset = movie_changeset |&gt; Ecto.Changeset.put_assoc(<span class="hljs-symbol">:actors</span>, [%{<span class="hljs-symbol">name:</span> <span class="hljs-string">&quot;Gary&quot;</span>}])                      
%Ecto.Changeset&lt;
  <span class="hljs-symbol">action:</span> <span class="hljs-keyword">nil</span>,
  <span class="hljs-symbol">changes:</span> %{
    <span class="hljs-symbol">actors:</span> [
      %Ecto.Changeset&lt;
        <span class="hljs-symbol">action:</span> <span class="hljs-symbol">:insert</span>,
        <span class="hljs-symbol">changes:</span> %{<span class="hljs-symbol">name:</span> <span class="hljs-string">&quot;Gary&quot;</span>},
        <span class="hljs-symbol">errors:</span> [],
        <span class="hljs-symbol">data:</span> <span class="hljs-comment">#Example.Actor&lt;&gt;,</span>
        <span class="hljs-symbol">valid?:</span> <span class="hljs-keyword">true</span>
      &gt;
    ]
  },
  <span class="hljs-symbol">errors:</span> [],
  <span class="hljs-symbol">data:</span> <span class="hljs-comment">#Example.Movie&lt;&gt;,</span>
  <span class="hljs-symbol">valid?:</span> <span class="hljs-keyword">true</span>
&gt;
iex&gt;  Repo.update!(changeset)
%Example.Movie{
  <span class="hljs-symbol">__meta__:</span> %Ecto.Schema.Metadata&lt;<span class="hljs-symbol">:loaded</span>, <span class="hljs-string">&quot;movies&quot;</span>&gt;,
  <span class="hljs-symbol">actors:</span> [
    %Example.Actor{
      <span class="hljs-symbol">__meta__:</span> %Ecto.Schema.Metadata&lt;<span class="hljs-symbol">:loaded</span>, <span class="hljs-string">&quot;actors&quot;</span>&gt;,
      <span class="hljs-symbol">id:</span> <span class="hljs-number">2</span>,
      <span class="hljs-symbol">movies:</span> %Ecto.Association.NotLoaded&lt;association <span class="hljs-symbol">:movies</span> is <span class="hljs-keyword">not</span> loaded&gt;,
      <span class="hljs-symbol">name:</span> <span class="hljs-string">&quot;Gary&quot;</span>
    }
  ],
  <span class="hljs-symbol">characters:</span> [],
  <span class="hljs-symbol">distributor:</span> <span class="hljs-keyword">nil</span>,
  <span class="hljs-symbol">id:</span> <span class="hljs-number">1</span>,
  <span class="hljs-symbol">tagline:</span> <span class="hljs-string">&quot;Something about video games&quot;</span>,
  <span class="hljs-symbol">title:</span> <span class="hljs-string">&quot;Ready Player One&quot;</span>
}
</code></pre>
<p>&#x4E00;&#x4E2A; ID &#x4E3A; &#x201C;2&#x201D;&#xFF0C;&#x8FDE;&#x540C;&#x6307;&#x5B9A;&#x5C5E;&#x6027;&#x7684;&#x65B0;&#x89D2;&#x8272;&#xFF0C;&#x5C31;&#x8FD9;&#x6837;&#x88AB;&#x521B;&#x5EFA;&#x51FA;&#x6765;&#x4E86;&#x3002;</p>
<p>&#x4E0B;&#x4E00;&#x7AE0;&#xFF0C;&#x6211;&#x4EEC;&#x5C06;&#x5B66;&#x4E60;&#x5982;&#x4F55;&#x67E5;&#x627E;&#x76F8;&#x5173;&#x8054;&#x7684;&#x8BB0;&#x5F55;&#x3002;</p>

                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="changesets.html" class="navigation navigation-prev " aria-label="Previous page: Changesets">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="querying.html" class="navigation navigation-next " aria-label="Next page: 查询">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"version":"1.1.1","title":"关联关系","level":"3.3","depth":1,"next":{"title":"查询","level":"3.4","depth":1,"path":"lessons/ecto/querying.md","ref":"lessons/ecto/querying.md","articles":[]},"previous":{"title":"Changesets","level":"3.2","depth":1,"path":"lessons/ecto/changesets.md","ref":"lessons/ecto/changesets.md","articles":[]},"dir":"ltr"},"config":{"gitbook":"*","theme":"default","variables":{},"plugins":[],"pluginsConfig":{"highlight":{},"search":{},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"}},"file":{"path":"lessons/ecto/associations.md","mtime":"2019-11-05T02:25:51.000Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2019-11-05T03:01:22.004Z"},"basePath":"../..","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="../../gitbook/gitbook.js"></script>
    <script src="../../gitbook/theme.js"></script>
    
        
        <script src="../../gitbook/gitbook-plugin-search/search-engine.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-search/search.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-sharing/buttons.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    

    </body>
</html>

